*********************************
*	Author: Rithika Kumar	    *
*   Table A5 (Appendix)         * 
*********************************


** Set working directory to "JOP Replication files" folder
** NOTE: Here I have created two separate balance tables since I cannot include the balance table for both years in the same command. Some variables ie. literate rural dil_dummy rel_caste1 rel_caste2 rel_caste3 rel_caste5  are constant across both years and have been exported with the balance table for year == 0. Other variables i.e. EW_Age EW_child good_health  INCOME vary across both years and hence have been separately exportd for year ==0 and year ==1. 

*. have combined both outputs in a seaprte file but the A5 table was eventually created manually because I made tweaks to the formatting on my Latex file. 
* The numbers are all in line witht eh output from this code. 


use "DATA FILES TO SHARE/TEMP_FILES/main_ew_hh_df.dta", clear
keep if did_sample == 1

gen rural = 0 
replace rural = 1 if URBAN == 0

gen literate = 0 
replace literate = 1 if EW_edu>0


tabulate GROUPS6, generate(rel_caste)

replace INCOME = . if INCOME<0 

gen good_health = 0 
replace good_health = 1 if EW_health <3

label define mighusb_lbl 0 "Co-resident Husband" 1 "Husband migrated between waves"
label values w2_abshusband_dummy mighusb_lbl

tab w2_abshusband_dummy


label var w2_abshusband_dummy "Migrant Status"
label var rural "Rural (%)"
label var literate "Literate (%)"
label var rel_caste1 "Upper Caste (%)"
label var rel_caste2 "OBC (%)"
label var rel_caste3 "Dalit (%)"
label var rel_caste5 "Muslim (%)"
label var dil_dummy "Daughter-in-law (%)"
label var EW_Age "Age (in years)"
label var EW_child "Number of children"
label var INCOME "Income (in Rs.)"
label var good_health "Good health (%)"

balancetable w2_abshusband_dummy rural literate dil_dummy rel_caste1 rel_caste2 rel_caste3 rel_caste5 EW_Age EW_child good_health  INCOME if year ==0  using `"OUTPUT/TABLES/Table_A5_w1.tex"', ctitles("Co-resident Husband" "Husband migrated between waves") replace varlabels wide(mean diff)

balancetable w2_abshusband_dummy EW_Age EW_child good_health INCOME if year ==1  using `"OUTPUT/TABLES/Table_A5_w2.tex"', ctitles("Co-resident Husband" "Husband migrated between waves") replace varlabels wide(mean diff)


cd "OUTPUT/TABLES/"

* Open the output LaTeX file for writing (this will create a new file or overwrite an existing one)
file open outfile using "Table_A5.tex", write text replace

* List of LaTeX files you want to append
local tex_files "Table_A5_w1.tex Table_A5_w2.tex"

* Loop through the files and append each to the output file
foreach file of local tex_files {
    * Open each LaTeX file to read
    file open infile using `file', read text
    
    * Read and append content to the final output file
    file read infile line
    while r(eof) == 0 {
        file write outfile `"`line'"' _n
        file read infile line
    }
    
    * Close the input file after reading
    file close infile
}

* Close the output file after appending all files
file close outfile




